package com.mng.JdbcTemplateTest;

import com.mng.JdbcTemplateTest.dao.EmployeeDao;
import com.mng.JdbcTemplateTest.model.Department;
import com.mng.JdbcTemplateTest.model.Employee;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class JdbcTest {

  //insert  data
  @Test
  public void test1(){
    ApplicationContext context = new ClassPathXmlApplicationContext("JdbcTemplateTest.xml");
    EmployeeDao employeeDao = context.getBean("employeeDao", EmployeeDao.class);
    Employee employee = new Employee(null,"mngg","1","13@qq",1L);
    employeeDao.add(employee);
  }
  //update data
  @Test
  public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("JdbcTemplateTest.xml");
    EmployeeDao employeeDao = context.getBean("employeeDao", EmployeeDao.class);
    Employee employee = new Employee(null,"mngg","1","1355@qq",1L);
    employeeDao.updateEmployee(employee);
  }
  //delete data
  @Test
  public void test3(){
    ApplicationContext context = new ClassPathXmlApplicationContext("JdbcTemplateTest.xml");
    EmployeeDao employeeDao = context.getBean("employeeDao", EmployeeDao.class);
    Employee employee = new Employee(null,"mngg","1","1355@qq",1L);
    employeeDao.deleteEmployee(employee);
  }
  //select basic class or object
  @Test
  public void test4(){
    ApplicationContext context = new ClassPathXmlApplicationContext("JdbcTemplateTest.xml");
    EmployeeDao employeeDao = context.getBean("employeeDao", EmployeeDao.class);
    //返回一个值
    Department department = new Department();
    department.setId(1L);
    Integer count = employeeDao.countEmployee(department);
    System.out.println(count);
    //返回一个对象
    Employee employee = employeeDao.findEmployee(3L);
    System.out.println(employee);

    //返回对象列表
    List<Employee> employeeList = employeeDao.findAllEmployee(department);
    System.out.println(employeeList);
  }
  // batch add/update/delete
  @Test
  public void test5(){
    ApplicationContext context = new ClassPathXmlApplicationContext("JdbcTemplateTest.xml");
    EmployeeDao employeeDao = context.getBean("employeeDao", EmployeeDao.class);
    Employee employee1 = new Employee(null,"mngg","1","1355@qq",1L);
    Employee employee2 = new Employee(null,"mngg2","0","1352135@qq",1L);
    List<Employee> employeeList = new ArrayList<>();
    employeeList.add(employee1);
    employeeList.add(employee2);
    employeeDao.batchAddEmployee(employeeList);
  }


}
